package com.example.productmanagerspringboot.controller;

import com.example.productmanagerspringboot.data.entity.Order;
import com.example.productmanagerspringboot.data.pojo.OrderIds;
import com.example.productmanagerspringboot.data.vo.OrderVO;
import com.example.productmanagerspringboot.data.vo.Response;
import com.example.productmanagerspringboot.data.vo.ResponseWithData;
import com.example.productmanagerspringboot.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orders")
@Slf4j
public class OrderController {

    private final OrderService service;

    public OrderController(OrderService service) {
        this.service = service;
    }


    @GetMapping("/list")
    @Operation(summary = "展示订单")
    //http://localhost:8080/api/goods/list?pageNumber=1&pageSize=10
    public ResponseEntity<ResponseWithData<OrderVO>> getOrders(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                               @RequestParam(value = "orderStatus", defaultValue = "-2") Integer orderStatus) {

        return ResponseEntity.ok(new ResponseWithData<>("success", 200, service.getOrders(pageNumber, pageSize, orderStatus)));
    }

    @PostMapping("/add")
    @Operation(summary = "添加订单")
    public ResponseEntity<Response> addOrder(@Validated @RequestBody Order order) {
        Response response = new Response();
        if (!service.addOrder(order)) {
            response.setResultCode(500);
            response.setMessage("新增失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        } else {
            response.setResultCode(200);
            response.setMessage("新增成功");
            return ResponseEntity.ok().body(response);
        }
    }

    @PutMapping("/start")
    @Operation(summary = "服务进行中")
    public ResponseEntity<Response> startService(@RequestBody OrderIds orderIds) {
        System.out.println(orderIds);
        return service.editOrder(3, orderIds.getIds());
    }

    @PutMapping("/completed")
    @Operation(summary = "服务完成")
    public ResponseEntity<Response> completedService(@RequestBody OrderIds orderIds) {
        return service.editOrder(4, orderIds.getIds());
    }

    @PutMapping("/close")
    @Operation(summary = "关闭订单")
    public ResponseEntity<Response> closeOrder(@RequestBody OrderIds orderIds) {
        return service.editOrder(-1, orderIds.getIds());
    }


}
